﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.Managers.CApExportToPlmxPackageManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX.Managers;

[Serializable]
public class CApExportToPlmxPackageManager
{
  [NonSerialized]
  private const string m_imageName = "preview.jpeg";
  [NonSerialized]
  private const string m_packageInfoFileName = "package.json";
  [NonSerialized]
  private string m_packageInfoFileFullPath = "";
  public CApExportToPlmxPackageManager.CApExportToPlmxProduct product = new CApExportToPlmxPackageManager.CApExportToPlmxProduct();
  public string createdAt = "";
  public string version = "1.0";
  public string createdBy = "";
  public string name = "";
  public string url = "";
  public string image = "";
  public int numOfExportedObjects = -1;

  public CApExportToPlmxPackageManager(string packageFolderPath, string packageName, int numOfObjs)
  {
    this.m_packageInfoFileFullPath = packageFolderPath + "\\package.json";
    this.image = packageFolderPath + "\\preview.jpeg";
    this.createdBy = Environment.UserName;
    this.createdAt = DateTime.Now.ToString();
    this.name = !(packageName != "") ? ((ITxObject) TxApplication.ActiveDocument.CurrentStudy).Name : packageName;
    this.numOfExportedObjects = numOfObjs;
  }

  public void CreatePackageInfo()
  {
    try
    {
      byte[] array;
      using (MemoryStream memoryStream = new MemoryStream())
      {
        new DataContractJsonSerializer(this.GetType()).WriteObject((Stream) memoryStream, (object) this);
        array = memoryStream.ToArray();
      }
      using (StreamWriter streamWriter = new StreamWriter(this.m_packageInfoFileFullPath, false))
        streamWriter.Write(Encoding.UTF8.GetString(array, 0, array.Length));
    }
    catch (Exception ex)
    {
      string message = ex.Message;
      throw ex;
    }
  }

  public void CreatStudyPreviewImage()
  {
    Bitmap bitmap = (Bitmap) null;
    try
    {
      TxGraphicViewer graphicViewer = TxApplication.ViewersManager.GraphicViewer;
      if (graphicViewer != null)
        bitmap = graphicViewer.GetImage(new Size(640, 480));
      bitmap?.Save(this.image, ImageFormat.Jpeg);
    }
    catch (Exception ex)
    {
      string message = ex.Message;
      throw ex;
    }
  }

  [Serializable]
  public class CApExportToPlmxProduct
  {
    public string type = "ps";
    public bool isProductIncluded;
    public string id = "";

    public CApExportToPlmxProduct()
    {
      this.id = Guid.NewGuid().ToString().ToUpper().Replace("-", "");
    }
  }
}
